package log

import (
	"context"
	"device-admin/internal/dal"
	"device-admin/internal/dao"
	"device-admin/internal/define"
	"device-admin/internal/models"
)

// OperationLog 操作日志
func OperationLogList(
	ctx context.Context,
	operationType define.OperationType, // 操作类型
	objID string, // 对象id
	associationID string, // 关联id
	lang string, // 语言,默认zh-cn
	startTime, endTime int64, // 开始和截止时间
	page, limit int, // 分页参数
) (
	[]*models.OperationLog,
	int64,
	error,
) {
	// 获取列表
	records, total, err := dao.OperationLog{}.List(
		ctx,
		dal.Q,
		operationType,
		objID,
		associationID,
		startTime,
		endTime,
		page,
		limit,
	)
	if err != nil {
		return []*models.OperationLog{}, 0, nil
	}
	// 操作日志的key翻译
	// 如: {"merchant":"大道深圳"}
	// 翻译为: {"商户":"大道深圳"}
	for index, record := range records {
		records[index].HistoryData = fieldsTrans(operationType, "zh-cn", record.HistoryData)
		records[index].CurrentData = fieldsTrans(operationType, "zh-cn", record.CurrentData)
	}
	return records, total, nil
}
